package com.guofen.service.hsmpy.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.HsmSmallWormMapper;
import com.guofen.pojo.HsmSmallWorm;
import com.guofen.service.hsmpy.SmallWormBreedingFeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SmallWormBreedingFeedServiceImpl implements SmallWormBreedingFeedService {

    @Autowired
    private HsmSmallWormMapper hsmSmallWormMapper;

    @Override
    public boolean add(Integer id, String small_bug_breed_time, String small_bug_breed_material, Double small_bug_breed_material_w,
                       String small_bug_breed_box_number,Double small_bug_breed_box_w, String small_bug_breed_box_remarks) {
        HsmSmallWorm hsmSmallWorm = hsmSmallWormMapper.selectByPrimaryKey(id);
        Map<String,HsmSmallWorm> map= hsmSmallWormMapper.getDate(id);
//        if(map.get("small_bug_breed_start_time")==null){
        if(map==null){
            hsmSmallWorm.setSmallBugBreedStartTime(small_bug_breed_time);
            hsmSmallWorm.setSmallBugBreedMaterial(small_bug_breed_material);
            hsmSmallWorm.setSmallBugBreedMaterialWeight(small_bug_breed_material_w);
            hsmSmallWorm.setSmallBugBreedBoxNumber(small_bug_breed_box_number);
            hsmSmallWorm.setSmallBugBreedBoxWeight(small_bug_breed_box_w);
            hsmSmallWorm.setSmallBugBreedRemarks(small_bug_breed_box_remarks);
        }
        else if (map.get("small_bug_breed_start_time")!=null && map.get("small_bug_breed_material_date_2")==null){
            hsmSmallWorm.setSmallBugBreedMaterialDate2(small_bug_breed_time);
            hsmSmallWorm.setSmallBugBreedMaterial(small_bug_breed_material);
            hsmSmallWorm.setSmallBugBreedMaterialWeight2(small_bug_breed_material_w);
            hsmSmallWorm.setSmallBugBreedBoxNumber(small_bug_breed_box_number);
            hsmSmallWorm.setSmallBugBreedBoxWeight2(small_bug_breed_box_w);
            hsmSmallWorm.setSmallBugBreedRemarks(small_bug_breed_box_remarks);
        }
        else if(map.get("small_bug_breed_start_time")!=null && map.get("small_bug_breed_material_date_2")!=null && map.get("small_bug_breed_material_date_3")==null){
            hsmSmallWorm.setSmallBugBreedMaterialDate3(small_bug_breed_time);
            hsmSmallWorm.setSmallBugBreedMaterial(small_bug_breed_material);
            hsmSmallWorm.setSmallBugBreedMaterialWeight3(small_bug_breed_material_w);
            hsmSmallWorm.setSmallBugBreedBoxNumber(small_bug_breed_box_number);
            hsmSmallWorm.setSmallBugBreedBoxWeight3(small_bug_breed_box_w);
            hsmSmallWorm.setSmallBugBreedRemarks(small_bug_breed_box_remarks);
        }
        else{
            return false;
        }

        int result = hsmSmallWormMapper.updateByPrimaryKey(hsmSmallWorm);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<Map<String, HsmSmallWorm>> getlatest() {
        return hsmSmallWormMapper.getSmallWormBreedingFeedLatest();
    }

    @Override
    public List<Map<String, HsmSmallWorm>> getlist() {
        List<Map<String, HsmSmallWorm>> smallWormBreedingFeedList = hsmSmallWormMapper.getSmallWormBreedingFeedList();
        List<Map<String, HsmSmallWorm>> list = new ArrayList<>();
        for (Map<String, HsmSmallWorm> smallWormBreedingFeed : smallWormBreedingFeedList) {
            // 从未进行小虫进料的数据
            if (smallWormBreedingFeed.get("smallBugBreedStartTime") == null) {
                list.add(smallWormBreedingFeed);
            }else {
                List<Map<String, HsmSmallWorm>> smallWormBreedingFeedList1 = hsmSmallWormMapper.getSmallWormBreedingFeedList1();
                if (smallWormBreedingFeedList1 != null) {
                    for (Map<String, HsmSmallWorm> stringHsmSmallWormMap : smallWormBreedingFeedList1) {
                        if (stringHsmSmallWormMap.equals(smallWormBreedingFeed)) {
                            list.add(stringHsmSmallWormMap);
                        }
                    }
                }
            }
        }

        return list;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<HsmSmallWorm> list = hsmSmallWormMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<HsmSmallWorm> list = hsmSmallWormMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return hsmSmallWormMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(HsmSmallWorm record) {
        return hsmSmallWormMapper.updateByPrimaryKeySelective(record);
    }
}
